<?php

namespace App\Http\Controllers\Innerend;


use App\Manage\SiteConfigManage;
use App\Models\Admin;

class SiteConfigController extends BaseController
{

    // 话题
    public function initialize()
    {
        parent::initialize();

        $this->assign('init_url', url('/innerend/site-config/setting'));
        $this->model = 'SiteInfo';
    }


    // 展示内容
    public function setting()
    {
        $manage  = new SiteConfigManage();
        $configs = $manage->getConfig();
        $formUrl = url('/innerend/site-config/on-save');

        $html = '';
        $form = $this->adminForm();
        //$status   = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        //$password = $form->formPassword('password', '', '密码');
        foreach($configs as $_config){
            $string = '';
            if($_config['type'] == 'input'){
                $string = $form->formInput($_config['field'], $_config['value'], $_config['name']);
            } elseif($_config['type'] == 'decimal'){
                $string = $form->formInput($_config['field'], $_config['value'], $_config['name']);
            } elseif($_config['type'] == 'radio'){
                $string = $form->formRadio($_config['field'], $_config['value'], json_decode($_config['extend'], true));
            }
            // TODO 其他

            $html .= $form->makeItem($_config['name'], $string, $_config['description']);
        }

        return $this->dispaly('form', ['formHtml' => $html, 'formUrl' => $formUrl]);
    }

    // 保存配置信息
    public function onSave()
    {
        $log = new SiteConfigManage();
        $res = $log->saveConfig();
        if($res === false){
            return $this->error($log->getError());
        }

        return $this->success();
    }
}


